//
//  UserInfoViewController.m
//  AppointmentForCar
//
//  Created by Herron on 16/10/4.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "UserInfoViewController.h"
#import "AppDelegate.h"
#import "CityListViewController.h"
#import "CameraTakeMamanger.h"
#import "CarBrandListViewController.h"
#import "IQActionSheetPickerView.h"
#import "PayTypeViewController.h"
#import "ProvinceListViewController.h"
#import "SeriesModel.h"


@interface UserInfoViewController ()<IQActionSheetPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UITextField *nicknameTextField;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (weak, nonatomic) IBOutlet UIImageView *licenseImageView;
@property (weak, nonatomic) IBOutlet UIImageView *driverLicImageView;
@property (weak, nonatomic) IBOutlet UILabel *carBrandLabel;
@property (weak, nonatomic) IBOutlet UIButton *licensePlateBtn;
@property (weak, nonatomic) IBOutlet UITextField *licensePlateTextField;
@property (nonatomic, copy) NSString *avatarUrl;            // 头像
@property (nonatomic, copy) NSString *licensePhotoUrl;      // 车牌
@property (nonatomic, copy) NSString *driverLicPhotoUrl;    // 驾照
@property (nonatomic, copy) NSString *cityId;               // 市区id
@property (nonatomic, copy) NSString *payType;              // 支付类型
@property (nonatomic, strong) SeriesModel *series;          // 车辆品牌

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"完善资料";
    self.phoneLabel.text = self.phone;
    self.payType = @"1";// 1支付宝(默认) 2微信
    
    NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
    [notification addObserver:self selector:@selector(selectCity:) name:@"sendCity" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadBrand:) name:@"sendBrandForRegister" object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <CityListViewControllerDelegate>
- (void)selectCity:(NSNotification *)notification {
    NSDictionary *dic = notification.userInfo;
    CityModel *city = [[CityModel alloc] initWithDictionary:dic error:nil];
    self.cityLabel.text = city.name;
    self.cityId = city.cityId;
}

#pragma mark - <IQActionSheetPickerViewDelegate>
-(void)actionSheetPickerView:(IQActionSheetPickerView *)pickerView didSelectTitles:(NSArray *)titles {
    [self.licensePlateBtn setTitle:[titles componentsJoinedByString:@""] forState:UIControlStateNormal];
}


#pragma mark - event mothed
// 注册
- (IBAction)paymentClick:(id)sender {
    if (!self.avatarUrl.length) {
        [HUDManager showWarningWithText:@"请上传头像"];
        return;
    }
    if (!self.nicknameTextField.text.length) {
        [HUDManager showWarningWithText:@"请输入昵称"];
        return;
    }
    if (!self.cityId.length) {
        [HUDManager showWarningWithText:@"请选择服务地区"];
        return;
    }
    if (!self.series.brandId.length) {
        [HUDManager showWarningWithText:@"请选择车辆品牌"];
        return;
    }
    if (self.licensePlateBtn.currentTitle.length<2) {
        [HUDManager showWarningWithText:@"请选择车牌类型"];
        return;
    }
    if (!self.licensePlateTextField.text.length) {
        [HUDManager showWarningWithText:@"请输入车牌号"];
        return;
    }
    if (!self.licensePhotoUrl.length) {
        [HUDManager showWarningWithText:@"请上传车牌照片"];
        return;
    }
    if (!self.driverLicPhotoUrl.length) {
        [HUDManager showWarningWithText:@"请上传驾驶证照片"];
        return;
    }
    NSDictionary *parameDic = @{@"driverPhoneNumber":self.phone,
                                @"driverAvatarUrl":self.avatarUrl,
                                @"driverNickName":self.nicknameTextField.text,
                                @"cityId":self.cityId,
                                @"seriesId":self.series.seriesId,
                                @"licensePlate":[NSString stringWithFormat:@"%@ %@", self.licensePlateBtn.currentTitle, self.licensePlateTextField.text],
                                @"licensePhotoUrl":self.licensePhotoUrl,
                                @"driverLicPhotoUrl":self.driverLicPhotoUrl,
                                @"payType":self.payType,
                                @"invitationCode":self.invitationCode};
    NSLog(@"parameDic = %@", parameDic);
    [HUDManager showLoadingHUDViewWithText:@"正在注册"];
    [NetWork postNetWorkWithUrl:kURLDriverRegister parame:parameDic successBlock:^(NSDictionary *dic) {
        NSLog(@"register = %@", dic);
        if ([dic[@"status"] isEqualToNumber:@(0)]) {
            NSString *price = [dic[@"data"][@"certifiedPrice"] stringValue];
            if ([price isEqualToString:@"0"]) {
                [HUDManager showWarningWithText:@"注册成功"];
                AppDelegate *appDelegate = kAppDelegate;
                [appDelegate setRootViewController];
            } else {
                [HUDManager hideHUDView];
                PayTypeViewController *vc = [[PayTypeViewController alloc] init];
                vc.userId = dic[@"data"][@"driverId"];
                vc.totalFee = price;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } else {
            [HUDManager showWarningWithText:dic[@"message"]];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:kMessage_NetError];
    }];
}

// 地区
- (IBAction)cityClick:(id)sender {
    ProvinceListViewController *vc = [[ProvinceListViewController alloc] init];
    vc.type = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}

// 头像
- (IBAction)uploadAvatarClick:(id)sender {
    [[CameraTakeMamanger sharedInstance] cameraSheetInController:self handler:^(UIImage *image, NSString *imagePath) {
        NSLog(@"imagePath = %@", imagePath);
        [NetWork uploadImageWithUrl:kURLUploadImage image:image imageName:@"avatar" successBlock:^(NSDictionary *dic) {
            NSLog(@"dic = %@", dic);
            NSArray *urls = dic[@"data"][@"urls"];
            self.avatarUrl = [urls firstObject];
            [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, self.avatarUrl]] placeholderImage:[UIImage imageNamed:@"registerAvatar"]];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"上传失败"];
        }];
    }];
}

// 车牌照片
- (IBAction)uploadLicensePhotoClick:(id)sender {
    [[CameraTakeMamanger sharedInstance] cameraSheetInController:self handler:^(UIImage *image, NSString *imagePath) {
        [NetWork uploadImageWithUrl:kURLUploadImage image:image imageName:@"license" successBlock:^(NSDictionary *dic) {
            NSLog(@"dic = %@", dic);
            NSArray *urls = dic[@"data"][@"urls"];
            self.licensePhotoUrl = [urls firstObject];
            [self.licenseImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, self.licensePhotoUrl]] placeholderImage:[UIImage imageNamed:@"example0"]];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"上传失败"];
        }];
    }];
}

// 驾驶证照片
- (IBAction)uploadDriverLicPhotoClick:(id)sender {
    [[CameraTakeMamanger sharedInstance] cameraSheetInController:self handler:^(UIImage *image, NSString *imagePath) {
        NSLog(@"imagePath = %@", imagePath);
        [NetWork uploadImageWithUrl:kURLUploadImage image:image imageName:@"driverLic" successBlock:^(NSDictionary *dic) {
            NSLog(@"dic = %@", dic);
            NSArray *urls = dic[@"data"][@"urls"];
            self.driverLicPhotoUrl = [urls firstObject];
            [self.driverLicImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, self.driverLicPhotoUrl]] placeholderImage:[UIImage imageNamed:@"example"]];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"上传失败"];
        }];
    }];
}

// 车辆品牌
- (IBAction)carBrandClick:(id)sender {
    CarBrandListViewController *vc = [[CarBrandListViewController alloc] init];
    vc.type = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}

// 这牌类型
- (IBAction)licensePlateClick:(id)sender {
    IQActionSheetPickerView *picker = [[IQActionSheetPickerView alloc] initWithTitle:@"车牌号码" delegate:self];
    [picker setTag:2];
    [picker setTitlesForComponents:@[@[@"京",@"津",@"沪",@"渝",@"冀",@"豫",@"云",@"辽",@"黑",@"湘",@"皖",@"鲁",@"新",@"苏",@"浙",@"赣",@"桂",@"甘",@"晋",@"蒙",@"陕",@"吉",@"闽",@"贵",@"粤",@"青",@"藏",@"琼"],
  @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U", @"V",@"W",@"X",@"Y",@"Z"]]];
    [picker show];
}

- (void)reloadBrand:(NSNotification *)notification {
    NSDictionary *dic = notification.userInfo;
    self.series = dic[@"brand"];
    self.carBrandLabel.text = self.series.name;
}

@end
